import requests
import random
import flask, json

server = flask.Flask("ITRA")


@server.route('/requestITRA')

def requestITRA():
    global echoToken
    echoToken = random.random()
    global results
    results = []
    requestITRA1()
    return json.dumps(results, ensure_ascii=False)


def requestITRA1():
    url = 'https://www.itra.run/api/runner/find'
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
    }
    name = flask.request.values.get('name')
    params = {
        "name": name,
        "start": len(results) + 1,
        "count": 50,
        "echoToken": echoToken
    }
    resp = requests.post(url, data=params, headers=headers).json()
    resultCount = resp['resultCount']
    results.extend(resp['results'])

    if resultCount > len(results):
        requestITRA1()


if __name__ == '__main__':
    server.run(debug=True)
